package com.example.baseweb.service.impl;

import com.example.baseweb.common.PageResponse;
import com.example.baseweb.dto.DishDTO;
import com.example.baseweb.entity.Dish;
import com.example.baseweb.repository.DishRepository;
import com.example.baseweb.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishRepository dishRepository;

    private DishDTO toDTO(Dish dish) {
        if (dish == null) return null;
        DishDTO dto = new DishDTO();
        dto.setId(dish.getId());
        dto.setName(dish.getName());
        dto.setCategoryId(dish.getCategoryId());
        dto.setPrice(dish.getPrice());
        dto.setDescription(dish.getDescription());
        dto.setImage(dish.getImage());
        return dto;
    }

    private List<DishDTO> toDTOList(List<Dish> list) {
        return list.stream().map(this::toDTO).collect(Collectors.toList());
    }

    @Override
    public List<DishDTO> getAllDishes() {
        return toDTOList(dishRepository.findAllOrderByCategory());
    }

    @Override
    public DishDTO getDishById(Long id) {
        return toDTO(dishRepository.findById(id).orElse(null));
    }

    @Override
    public DishDTO saveDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        dish.setId(dishDTO.getId());
        dish.setName(dishDTO.getName());
        dish.setCategoryId(dishDTO.getCategoryId());
        dish.setPrice(dishDTO.getPrice());
        dish.setDescription(dishDTO.getDescription());
        dish.setImage(dishDTO.getImage());
        if (dish.getId() == null) {
            dish.setCreateTime(LocalDateTime.now());
            dish.setSalesCount(0);
        }
        dish.setUpdateTime(LocalDateTime.now());
        // 默认值
        if (dish.getStatus() == null) dish.setStatus(1);
        if (dish.getStock() == null) dish.setStock(-1);
        if (dish.getSalesCount() == null) dish.setSalesCount(0);
        Dish saved = dishRepository.save(dish);
        return toDTO(saved);
    }

    @Override
    public DishDTO updateDish(DishDTO dishDTO) {
        Dish existingDish = dishRepository.findById(dishDTO.getId()).orElse(null);
        if (existingDish == null) {
            throw new RuntimeException("菜品不存在");
        }
        
        existingDish.setName(dishDTO.getName());
        existingDish.setCategoryId(dishDTO.getCategoryId());
        existingDish.setPrice(dishDTO.getPrice());
        existingDish.setDescription(dishDTO.getDescription());
        existingDish.setImage(dishDTO.getImage());
        existingDish.setUpdateTime(LocalDateTime.now());
        
        Dish saved = dishRepository.save(existingDish);
        return toDTO(saved);
    }

    @Override
    public void deleteDish(Long id) {
        dishRepository.deleteById(id);
    }

    @Override
    public DishDTO updateDishStatus(Long id, Integer status) {
        Dish dish = dishRepository.findById(id).orElse(null);
        if (dish != null) {
            dish.setStatus(status);
            dish.setUpdateTime(LocalDateTime.now());
            return toDTO(dishRepository.save(dish));
        }
        return null;
    }

    @Override
    public List<DishDTO> getDishesByCategory(Long categoryId) {
        return toDTOList(dishRepository.findByCategoryIdOrderByCreateTimeDesc(categoryId));
    }

    @Override
    public List<DishDTO> getDishesByStatus(Integer status) {
        return toDTOList(dishRepository.findByStatus(status));
    }

    @Override
    public PageResponse<DishDTO> findDishPage(Long categoryId, int page, int size) {
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Page<Dish> dishPage = dishRepository.findByCategoryId(categoryId, pageRequest);
        List<DishDTO> dtoList = toDTOList(dishPage.getContent());
        return PageResponse.of(dtoList, dishPage.getTotalElements(), size, page);
    }
} 